<template>
  <a-row :gutter="10">
    <a-col :md="0" :sm="24">
      <a-card :bordered="false">
        <!-- 按钮操作区域 -->
        <a-row style="  margin-left: 14px;">
          <!--          <a-button v-has="'category:add'" @click="handleAddCateGory(1)" type="primary">添加</a-button>-->
        </a-row>
        <div style="background: #fff;padding-left:16px;height: 100%; margin-top: 5px">
          <!--          <a-input-search @search="onSearch" style="width:100%;margin-top: 10px" placeholder="请输入单位名称" />-->
          <!-- 树-->
<!--          <a-col :md="10" :sm="24">-->
<!--            <template>-->
<!--              <a-dropdown :trigger="[this.dropTrigger]" @visibleChange="dropStatus">-->
<!--               <span style="user-select: none">-->
<!--&lt;!&ndash;                     @check="onCheck"&ndash;&gt;-->
<!--                 &lt;!&ndash;                  multiple   &ndash;&gt;-->

<!--            <a-tree-->

<!--              @select="onSelect"-->
<!--              :selectedKeys="selectedKeys"-->
<!--              @rightClick="rightHandle"-->

<!--              :checkedKeys="checkedKeys"-->
<!--              :treeData="departTree"-->
<!--              :checkStrictly="checkStrictly"-->
<!--              :expandedKeys="iExpandedKeys"-->
<!--              :autoExpandParent="autoExpandParent"-->
<!--              @expand="onExpand"/>-->
<!--                </span>-->
<!--                &lt;!&ndash;新增右键点击事件,和增加添加和删除功能&ndash;&gt;-->
<!--                <a-menu slot="overlay">-->
<!--                  <a-menu-item v-has="'category:add'" @click="handleAddCateGory(2)" key="1">添加下级</a-menu-item>-->
<!--                  <a-menu-item v-has="'category:edit'" @click="handleEditCateGory" key="2">编辑</a-menu-item>-->
<!--                  <a-menu-item v-has="'category:del'" @click="handleDeleteCateGory" key="3">删除</a-menu-item>-->
<!--                  <a-menu-item @click="closeDrop" key="4">取消</a-menu-item>-->
<!--                </a-menu>-->
<!--              </a-dropdown>-->
<!--            </template>-->
<!--          </a-col>-->
        </div>
      </a-card>
    </a-col>
    <a-col :md="24" :sm="24">
      <a-card :bordered="false">
        <!-- 查询区域 -->
        <div class="table-page-search-wrapper">
          <!--          @keyup.enter.native="searchQuery"-->
          <a-form layout="inline">
            <a-row :gutter="24">

              <a-col :xl="6" :lg="7" :md="8" :sm="24">

                <a-form-item label="物资名称" :labelCol="labelCol" :wrapperCol="wrapperCol" >
                  <a-input placeholder="请输入物资名称" v-model="queryParam.name"></a-input>
                </a-form-item>
              </a-col>
              <a-col :xl="6" :lg="7" :md="8" :sm="24">

                <a-form-item label="物资规格" :labelCol="labelCol" :wrapperCol="wrapperCol" >
                  <a-input placeholder="请输入物资规格" v-model="queryParam.specifications"></a-input>
                </a-form-item>
              </a-col>
              <a-col :xl="6" :lg="7" :md="8" :sm="24">
                <a-form-item label="请选择物资分类" >
                  <j-search-select-tag  v-model="queryParam.assetType"  @change="change" :disabled="flagType"   type="list"
                                       :trigger-change="true"  dict="gf_zcgl_asset_type"
                                       placeholder="请选择物资分类"/>
                </a-form-item>

              </a-col>
              <a-col  v-show="queryParam.assetType == 'BGYP01'"   :xl="6" :lg="7" :md="8" :sm="24">
                <a-form-item label="请选择办公子分类">
                  <j-search-select-tag    type="list" v-model="queryParam.bgypType"
                                          :trigger-change="true"  dict="bgyp_type"
                                          placeholder="请选择子分类"/>
                </a-form-item>

              </a-col>
              <a-col  v-show="queryParam.assetType == 'CHWZ01'"   :xl="6" :lg="7" :md="8" :sm="24">
                <a-form-item label="请选择存货物资分类">
                  <j-search-select-tag    type="list" v-model="queryParam.chwzType"
                                          :trigger-change="true"  dict="chwz_type" :disabled="disableSubmit"
                                          placeholder="请选择子分类"/>
                </a-form-item>

              </a-col>

              <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
<!--              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>-->
                                  <a-button v-has="'assetFixed:export'" type="primary" icon="download"
                                            style="margin-left: 8px"       @click="handleExportXls('物资')">导出
                                  </a-button>
              <!--              <a @click="handleToggleSearch" style="margin-left: 8px">-->
              <!--                {{ toggleSearchStatus ? '收起' : '展开' }}-->
              <!--                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />-->
              <!--              </a>-->
            </span>
              </a-col>
<!--              <a-col :xl="6" :lg="7" :md="8" :sm="24">-->

<!--                <a-form-item style="width: 500px" label="请选择库存类型" :labelCol="labelCol" :wrapperCol="wrapperCol">-->

<!--                  <a-radio-group style="width: 300px" @change="searchQuery" v-model="queryParam.safeType">-->
<!--                    <a-radio-button value="all">全部</a-radio-button>-->
<!--                    <a-radio-button value="safe">低于安全库存物资</a-radio-button>-->

<!--                  </a-radio-group>-->
<!--                </a-form-item>-->
<!--              </a-col>-->


            </a-row>
          </a-form>
        </div>
        <!-- 操作按钮区域 -->
        <div class="table-operator">
          <!--          <a-button v-has="'assetFixed:add'" @click="handleAdd" type="primary" icon="plus">新增</a-button>-->
          <!--          </a-button>-->
          <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader"
                    :action="importExcelUrl" @change="handleImportExcel">
            <!--            <a-button v-has="'assetFixed:import'" type="primary" icon="import">导入</a-button>-->
          </a-upload>
          <!--          <a-button v-has="'assetFixed:temp'" type="primary" icon="download"-->
          <!--                    @click="handleExportXlsTemplate('固定资产信息模板')">模板下载-->
          <!--          </a-button>-->
          <a-dropdown v-if="selectedRowKeys.length > 0">
            <a-menu slot="overlay">
              <a-menu-item key="1" @click="batchDel" v-has="'assetFixed:del'">
                <a-icon type="delete"/>
                删除
              </a-menu-item>
            </a-menu>
            <a-button style="margin-left: 8px"> 批量操作
              <a-icon type="down"/>
            </a-button>
          </a-dropdown>
        </div>
        <!-- table区域 - begin   begin  start   end -->
        <div>
<!--          <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">-->
<!--            <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a-->
<!--            style="font-weight: 600">{{ selectedRowKeys.length }}</a>项-->
<!--            <a style="margin-left: 24px" @click="onClearSelected">清空</a>-->
<!--          </div>-->
          <!--            :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"-->
          <a-table
            :rowClassName="tableRowClassName"
            ref="table"
            size="middle"
            bordered
            rowKey="id"
            :columns="columns"
            :dataSource="dataSource"
            :pagination="ipagination"
            :loading="loading"
            class="j-table-force-nowrap"
            @change="handleTableChange">
            <!--            :scroll="{ x: 4000, y: screenHeight }"-->

            <span slot="action" slot-scope="text, record">
          <a v-has="'assetFixed:edit'" @click="handleEdit(record)">编辑</a>
            <a-divider type="vertical"/>
            <a-popconfirm v-has="'assetFixed:del'" title="确定删除吗?"
                          @confirm="() => handleDelete(record.id)"> <a>删除</a> </a-popconfirm>
            <template v-if="record.status === '0'">
            <a-divider type="vertical"/>
            <a v-has="'assetFixed:takeEffect'" @click="modifyForm(record,'资产生效')">资产生效</a>
          </template>
        </span>
            <template slot="imgSlot" slot-scope="text">
              <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
              <img v-else :src="getImgView(text)" height="25px" alt=""
                   style="max-width:80px;font-size: 12px;font-style: italic;"/>
            </template>
          </a-table>
        </div>
        <!-- table区域-end -->
        <!-- 表单区域 -->
        <assetFixed-modal ref="modalForm" @ok="modalFormOk"></assetFixed-modal>
        <asset-fixed-category-modal ref="categoryModal" @ok="modalFormOk"></asset-fixed-category-modal>
        <a-modal :title="lcModa.title" v-model="lcModa.visible" :footer="null" :maskClosable="false" width="80%">
          <component :disabled="lcModa.disabled" v-if="lcModa.visible" :is="lcModa.formComponent"
                     :processData="lcModa.processData" :isNew="lcModa.isNew"
                     @afterSubmit="processFormOk" @close="lcModa.visible=false, lcModa.disabled = false"></component>
        </a-modal>
      </a-card>
    </a-col>
  </a-row>
</template>
<script>
import store from '@/store'
import '@/assets/less/TableExpand.less'
import AssetFixedModal from './modules/AssetFixedModal'
import { JeecgListMixin } from '@/mixins/componentMix'
import JTreeTable from '@/components/jeecg/JTreeTable'
import { searchByKeywords } from '@/api/api'
import { getAction, deleteAction } from '@/api/manage'
import AssetFixedCategoryModal from './modules/AssetFixedCategoryModal'
import JSelectCategory from '../../components/jeecgbiz/JSelectCategory'
import { activitiMixin } from '@views/activiti/mixins/activitiMixin'
import { mixinDevice } from '@/utils/mixin'
import JSearchSelectTag from '../../components/dict/JSearchSelectTag'

export default {
  name: 'AssetFixedList',
  mixins: [activitiMixin, JeecgListMixin, mixinDevice],
  components: {
    AssetFixedCategoryModal,
    AssetFixedModal, JTreeTable,
    JSelectCategory, JSearchSelectTag
  },
  data () {
    return {

      description: '固定资产管理页面',
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          align: 'center',
          width:77,
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '库房名称',
          align: 'center',
          width:257,
          sorter: true,
          dataIndex: 'assetFixedCategoryId_dictText'
        },

        {
          title: '物资名称',
          align: 'center',
          width:257,
          dataIndex: 'name'
        },
        {
          title: '物资编码',
          align: 'center',
          ellipsis: true,
          width:117,
          dataIndex: 'code'
        },

        {
          title: '规格型号',
          align: 'center',
          ellipsis: true,
          width:117,
          dataIndex: 'specifications'
        },

        {
          title: '单位',
          align: 'center',
          width:117,
          dataIndex: 'unit'
        },
        {
          title: '数量',
          align: 'center',
          width:117,
          sorter: true,
          dataIndex: 'amount'
        },
        {
          title: '单价',
          align: 'center',
          width:117,
          sorter: true,
          dataIndex: 'price'
        },
        {
          title: '合计',
          align: 'center',
          dataIndex: 'total',
          width:117,
          customRender: function (t, r, index) {
            return (r.price * r.amount).toFixed(2)
          }
        },
        // {
        //   title: '库存预警',
        //   align: 'center',
        //   width:117,
        //   dataIndex: 'jiexian'
        // },
        {
          title: '备注',
          align: 'center',
          width:117,
          dataIndex: 'remark'
        },
        {
          title: '图片',
          align: 'center',
          width:117,
          dataIndex: 'image',
          scopedSlots: { customRender: 'imgSlot' }
        },
        // {
        //   title: '操作',
        //   dataIndex: 'action',
        //   align: 'center',
        //   fixed: 'right',
        //   width: 110,
        //   scopedSlots: { customRender: 'action' }
        // }
      ],
      url: {
        list: '/zcgl/assetFixed/ckList',
        delete: '/zcgl/assetFixed/delete',
        deleteBatch: '/zcgl/assetFixed/deleteBatch',
        exportXlsUrl: 'zcgl/assetFixed/exportXls1',
        importExcelUrl: 'zcgl/assetFixed/importExcel',
        exportXlsTemplate: 'zcgl/assetFixed/exportXlsTemplate'
      },
      currSelected: {},
      dropTrigger: '',
      selectedKeys: [],
      checkedKeys: [],
      departTree: [],
      checkStrictly: true,
      iExpandedKeys: [],
      autoExpandParent: true,
      rightClickSelectedKey: '',
      rightClickSelectedModel: '',
      categoryModel: {},
      //控制办公搜索的
      bgypTypeFlag:false,
      flagType:false,
    }
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  created () {

    this.queryParam.type = "2"

    let companyName = store.getters.userInfo.companyName

    getAction('/zcgl/assetFixedCategory/list?name='+companyName).then((res) => {
      if (res.success) {


        if(res.result.records[0]!=null){
          this.queryParam.assetFixedCategoryId = res.result.records[0].id
          this.searchQuery()

        }else{
          this.searchQuery()

        }


      }
    })


  },
  methods: {
    loadData (arg) {
      this.refresh()
      if (!this.url.list) {
        this.$message.error('请设置url.list属性!')
        return
      }
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1
      }
      var params = this.getQueryParams()//查询条件
      this.loading = true
      getAction(this.url.list, params).then((res) => {
        if (res.success) {
          this.dataSource = res.result.records
          this.ipagination.total = res.result.total
        }
        if (res.code === 510) {
          this.$message.warning(res.message)
        }
        this.loading = false
      })
    },
    //行内颜色
    tableRowClassName (record, index) {
      if (record.amount < record.jiexian) {
        return 'blue'
      }
    },
    loadTree () {
      var that = this
      that.treeData = []
      that.departTree = []
      getAction('/zcgl/assetFixedCategory/queryAllCkTreeList').then((res) => {
        if (res.success) {
          //部门全选后，再添加部门，选中数量增多
          this.allTreeKeys = []
          for (let i = 0; i < res.result.length; i++) {
            let temp = res.result[i]
            that.treeData.push(temp)
            // if (temp.orgCategory == 0){
            if (!temp.parentId || temp.parentId == 0) {
              temp.disableCheckbox = true
              temp.selectable = true
              // temp.disabled = true;
            }
            that.departTree.push(temp)
            that.setThisExpandedKeys(temp)
          }
          this.loading = false
        }
      })
    },
    setThisExpandedKeys (node) {
      if (node.children && node.children.length > 0) {
        this.iExpandedKeys.push(node.key)
        for (let a = 0; a < node.children.length; a++) {
          this.setThisExpandedKeys(node.children[a])
        }
      }
    },
    refresh () {
      this.loading = true
      this.loadTree()
    },
    // 右键点击下拉框改变事件
    dropStatus (visible) {
      if (visible == false) {
        this.dropTrigger = ''
      }
    },
    onCheck (checkedKeys, info) {
      this.hiding = false
      //this.checkedKeys = checkedKeys.checked
      // <!---- author:os_chengtgen -- date:20190827 --  for:切换父子勾选模式 =======------>
      if (this.checkStrictly) {
        this.checkedKeys = checkedKeys.checked
      } else {
        this.checkedKeys = checkedKeys
      }
      // <!---- author:os_chengtgen -- date:20190827 --  for:切换父子勾选模式 =======------>
    },
    change(value){
      this.queryParam.assetType = value

      // alert(value)
      // alert( this.queryParam.assetType)
    },
    onSelect (selectedKeys, e) {
      this.hiding = false
      let record = e.node.dataRef
      this.currSelected = Object.assign({}, record)
      this.categoryModel = this.currSelected
      this.selectedKeys = [record.key]
      this.categoryModel.parentId = record.parentId

      if (record.name == '本级库房') {
        this.queryParam.assetFixedCategoryId = ''
        delete this.queryParam.assetType
        this.flagType = false
      } else {
        this.queryParam.assetFixedCategoryId = record.id
        this.flagType = true

        if (record.name == '办公用品库') {
          // this.bgypTypeFlag = true

          this.queryParam.assetType = "BGYP01"
        }else  if (record.name == '易耗品库房'){

          this.queryParam.assetType = "YHP01"
          // this.bgypTypeFlag = false

        }else  if (record.name == '存货物资库房'){


          this.queryParam.assetType = "CHWZ01"
          // this.bgypTypeFlag = false
        }else{
          this.queryParam.assetType = ""

        }

      }


      this.loadData()
    },
    // 右键操作方法
    rightHandle (node) {
      this.dropTrigger = 'contextmenu'
      console.log(node.node.eventKey)
      this.rightClickSelectedKey = node.node.eventKey
      this.rightClickSelectedModel = node.node.dataRef
    },
    onExpand (expandedKeys) {
      console.log('onExpand', expandedKeys)
      // if not set autoExpandParent to false, if children expanded, parent can not collapse.
      // or, you can remove all expanded children keys.
      this.iExpandedKeys = expandedKeys
      this.autoExpandParent = false
    },
    // 右键店家下拉关闭下拉框
    closeDrop () {
      this.dropTrigger = ''
    },
    handleAddCateGory (mark) {
      console.log('mark', mark)
      if (mark === 2) {
        console.log('this.rightClickSelectedModel', this.rightClickSelectedModel)
        this.$refs.categoryModal.parentId = this.rightClickSelectedModel.id
        this.$refs.categoryModal.level = parseInt(this.rightClickSelectedModel.level) + 1
      } else if (mark === 1) {
        console.log('bbbbbbb')
        this.$refs.categoryModal.parentId = 0
        this.$refs.categoryModal.level = 1
      }
      this.$refs.categoryModal.visible = true
      this.$refs.categoryModal.add()
    },
    handleEditCateGory () {
      this.$refs.categoryModal.parentId = this.rightClickSelectedModel.parentId
      this.$refs.categoryModal.level = this.rightClickSelectedModel.level
      this.$refs.categoryModal.edit(this.rightClickSelectedModel)
      this.$refs.categoryModal.title = '编辑'
      this.$refs.categoryModal.disableSubmit = false
    },
    handleDeleteCateGory () {
      let that = this
      deleteAction('/zcgl/assetFixedCategory/delete', { id: this.rightClickSelectedModel.id }).then((res) => {
        if (res.success) {
          that.$message.success(res.message)
          that.loadData()
        } else {
          that.$message.warning(res.message)
        }
      })
    },
    modifyForm (r, name, type) {
      let tableData = {
        routeName: '@/views/activiti/form/AssetFixedTakeEffect',
        tableName: 'zcgl_asset_fixed',
        tableId: r.id
      }
      this.processCreateCom(tableData, '发起资产' + name + '流程', r.processStatus)
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';

/deep/ .ant-table-tbody .blue {
  /*background-color: #ff00b7 !important;*/
  color: red;
}

</style>